from django.shortcuts import render
# Create your views here.
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from carts.serializers import AddCartsSerializer, GetCartsSerialier, DeteleCartsSerializer, CheckAllSerializer
from carts.utils import ReadWriteTool
from goods.models import SKU


class AddCartView(ReadWriteTool, GenericAPIView):
    # 1.登陆用户吧商品添加到redis中

    serializer_class = AddCartsSerializer

    def post(self, request):
        data = request.data
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)

        sku_id = serializer.validated_data.get('sku_id')
        count = serializer.validated_data.get('count')
        selected = serializer.validated_data.get('selected')

        carts_dict = self.read_carts(request)

        # carts_dict = {'sku_id': [1, True], 'sku_id2': [2, False]}
        if sku_id in carts_dict.keys():
            carts_dict[sku_id][0] += count
            carts_dict[sku_id][1] = selected
        else:
            carts_dict[sku_id] = [count, selected]

        response = Response(serializer.data)

        self.write_carts(request, response, carts_dict)

        return response

    def get(self, request):
        carts_dict = self.read_carts(request)
        # carts_dict = {'sku_id': [1, True], 'sku_id2': [2, False]}
        skus = SKU.objects.filter(id__in=carts_dict.keys())
        for sku in skus:
            sku.count = carts_dict[sku.id][0]
            sku.selected = carts_dict[sku.id][1]

        serializer = GetCartsSerialier(skus, many=True)

        return Response(serializer.data)

    def put(self, requset):
        data = requset.data

        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)

        sku_id = serializer.validated_data.get('sku_id')
        count = serializer.validated_data.get('count')
        selected = serializer.validated_data.get('selected')

        carts_dict = self.read_carts(requset)
        # carts_dict = {'sku_id': [1, True], 'sku_id2': [2, False]}

        carts_dict[sku_id][0] = count
        carts_dict[sku_id][1] = selected

        response = Response(serializer.data)

        self.write_carts(requset, response, carts_dict)

        return response

    def delete(self, request):
        data = request.data
        serializer = DeteleCartsSerializer(data=data, many=True)
        serializer.is_valid(raise_exception=True)

        sku_id = serializer.validated_data.get('sku_id')

        carts_dict = self.read_carts(request)
        try:
            del carts_dict[sku_id]
        except:
            return Response({
                'message': '购物车中不存在这款商品',
            }, status=status.HTTP_400_BAD_REQUEST)

        response = Response({'message': '删除成功'}, status=status.HTTP_204_NO_CONTENT)

        self.write_carts(request, response, carts_dict)

        return response


class CheckAll(ReadWriteTool, GenericAPIView):
    serializer_class = CheckAllSerializer

    def put(self, request):
        s = self.get_serializer(data=request.data)
        s.is_valid(raise_exception=True)

        carts_dict = self.read_carts(request)
        # carts_dict = {'sku_id': [1, True], 'sku_id2': [2, False]}

        for item in carts_dict.values():
            item[1] = s.validated_data.get('selected')

        response = Response({'message': 'OK'})

        self.write_carts(request, response, carts_dict)

        return response
